import Util.EncryptUtils;
import Util.HttpClientUtil;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import enums.ReturnValue;

import java.util.HashMap;
import java.util.Map;

import static Util.StringUtils.parseJsonResponse;

public class SubApi {
    public static void main(String[] args) throws Exception {

        //商户私钥
        String privateKeyStr = "";
        //汇付宝公钥
        String publicKeyStr = "";


        String version = "1.0";
        String agent_id = "";
        String out_trade_no = "";
        String sub_account_id = "";
        String cash_fen = "1";
        String note = "测试提现";
        String notify_url = "";

        JSONObject body = new JSONObject();
        body.put("version",version);
        body.put("agent_id",agent_id);
        body.put("out_trade_no",out_trade_no);
        body.put("sub_account_id",sub_account_id);
        body.put("cash_fen",cash_fen);
        body.put("note",note);
        body.put("notify_url",notify_url);

        //RSA2签名
        String c = String.valueOf(body);
        ReturnValue signed = EncryptUtils.makeRSA2Sign(privateKeyStr,c,"utf-8");
        System.out.println("签名前参数---------->"+c);
        System.out.println("签名后参数---------->"+signed);

        JSONObject body1 = new JSONObject();
        body1.put("sign",String.valueOf(signed));
        body1.put("body",body);

        //RSA2加密
        String a = String.valueOf(body1);
        ReturnValue businessContext = EncryptUtils.rsaEncrypt(publicKeyStr,a,"utf-8");
        System.out.println("加密前参数---------->"+a);
        System.out.println("加密后参数---------->"+businessContext);


        //组装最终请求并输出
        Map<String,String> BodyMap = new HashMap<>();
        BodyMap.put("agentid",agent_id);
        BodyMap.put("businessContext", String.valueOf(businessContext));
        String HttpResult = HttpClientUtil.sendPostJson("https://www.heepay.com/API/Agent/AgentSubCash.aspx", BodyMap);
        System.out.println("接口返回参数---------->"+HttpResult);

        JsonNode jsonNode = parseJsonResponse(HttpResult);
        // 获取参数
        String data = jsonNode.get("businessContext").asText();
        //RSA2解密
        ReturnValue DecryptData = EncryptUtils.rsaDecrypt(privateKeyStr,data,"utf-8");
        System.out.println("返回参数解密后----------->"+DecryptData);

        }
    }
